

import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';

import 'package:dio/dio.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


import 'Address.dart';
import 'package:get/route_manager.dart';



class DioUtils{
  // ignore: non_constant_identifier_names
  // static final String BASE_URL = Address.homeHost;
  static DioUtils _instance;
  Dio _dio;
  BaseOptions _baseOptions;

  static DioUtils getInstance() {
    if (_instance == null) {
      _instance = new DioUtils();
    }
    return _instance;
  }

  DioUtils({String url}) {
    //请求参数配置
    _baseOptions = new BaseOptions(
      contentType:'application/json',
      baseUrl: url,
      connectTimeout: 80000,
      receiveTimeout: 80000,
      headers: {
        //预设好的header信息
        // "testHeader":"bb"
      },
      // contentType: ContentType.json,
      // responseType: ResponseType.json,

    );

    //创建dio实例
    _dio = new Dio(_baseOptions);

  }



  /// get请求

  get(url, {data, options,context}) async {
    print('get request path ------${url}-------请求参数${data}');
    print('------------');
    Response response;
    try {
      response = await _dio.get(url, queryParameters: data, options: options);
      print('get result ---${response.data}');
    } on DioError catch (e) {
      print('请求失败---错误类型${e.type}--错误信息${e.message}');
      // Toast.toast(context,msg: '错误信息${e.message}');

    }

    return response.data.toString();
  }

  postModel(url,{var params,var model,var function_name})async{

    var comParams = {
      'access_token':await PersistentStorage().getStorage('acctoken'),
      'uid':await PersistentStorage().getStorage('uid'),
      'partner_id':await PersistentStorage().getStorage('partner_id'),
      'model': model,
      'function_name': function_name,               // #用户接口名称
    };
    /// 公共擦参数
    _baseOptions.queryParameters = comParams;

    Response response;


    print(' ${await PersistentStorage().getStorage('urlStr')}${url}');

    print('request path ------${url}-------请求参数${comParams} 请求参数${params}');

    try {

      response = await _dio.post(url, queryParameters: params,);

      print('post result ---${response.data}');



      Map json = jsonDecode(response.data);

      // if(json['errmsg'] == 'no'){
      //
      //   BotToast.showText(text: json['message']);
      //
      // }


      if(json['errcode']==401||json['errcode']==1001){
        // Get.offAll(LoginPage());
      }


    }on DioError catch(e){



      // print('请求失败---错误类型${e.type}--错误信息${e.message}');


      // Toast.toast(Get.overlayContext,msg: '错误信息${e.message}',position: ToastPostion.ce nter);
      // BotToast.showText(text: '错误信息${e.message}');


      // Get.back();
      // Navigator.pop(context);

    }

    return response.data.toString();

  }

  post(url, {data, options,context}) async {


    print('请求地址 ------${_baseOptions.baseUrl}${url}   请求参数${data}');



    Response response;
    try {

      response = await _dio.post(url, queryParameters: data, options: options);

      print('返回结构 ---${response.data}');

      Map json = jsonDecode(response.data);


      if(Get.isDialogOpen){
        Get.back();
      }



      if(json['errmsg'] == 'no'){

        // Toast.toast(context,msg: json['message']);

        BotToast.showText(text: json['message']);
        // return;
      }


      if(json['errcode']==401||json['errcode']==1001){

        // Get.offAll(LoginPage());
      }

      BotToast.closeAllLoading();

    }on DioError catch(e){


      if(Get.isDialogOpen){
        Get.back();
      }


      print('请求失败---错误类型${e.type}--错误信息${e.message}');

      // Toast.toast(Get.overlayContext,msg: '错误信息${e.message}',position: ToastPostion.center);

      BotToast.showText(text: '错误信息${e.message}',);

      BotToast.closeAllLoading();

      // Get.back();
      // Navigator.pop(context);

    }

    return response.data.toString();
  }
}
